﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Biblio2.GUI;

namespace Biblio2.LocalParsing
{
    public partial class FileListEditUserControl : UserControl
    {
        #region Attribut
        private LocalParseResultDataRowComparer m_localParseResultDataRowComparer;
        private Color m_colorCritical = Color.LightSalmon;
        private Color m_colorOptional = Color.LemonChiffon;
        private Color m_colorComplete = Color.LightGreen;
        private Dictionary<String, Color> m_dicoColorColumn;
        private List<int> m_previousSelectedRows;
        private int m_previousSelectedColumn = 0;
        private int m_previousSelectedRow = 0;
        private int m_nbComplete = 0;
        private int m_nbOptional = 0;
        private int m_nbCritical = 0;
        #endregion Attribut
    
        #region Constructeur
        public FileListEditUserControl()
        {
            InitializeComponent();

            InitColorColumns();

            m_localParseResultDataRowComparer = new LocalParseResultDataRowComparer();

            m_previousSelectedRows = new List<int>();
        }

        private void InitColorColumns()
        {
            m_dicoColorColumn = new Dictionary<string, Color>();
            m_dicoColorColumn.Add(LocalParseResult.csSerieName, m_colorCritical);
            m_dicoColorColumn.Add(LocalParseResult.csSeasonIndex, m_colorCritical);
            m_dicoColorColumn.Add(LocalParseResult.csEpisodeIndex, m_colorCritical);
            m_dicoColorColumn.Add(LocalParseResult.csEpisodeFormat, m_colorOptional);
            m_dicoColorColumn.Add(LocalParseResult.csEpisodeTeam, m_colorOptional);
        }
        #endregion Constructeur

        #region Privées
        private void AddItem(LocalParseResult aResult)
        {
            DataGridViewRow row = new DataGridViewRow();
            row.Tag = aResult;
            DataGridViewCell cell;

            // Import
            cell = new DataGridViewCheckBoxCell();
            cell.Value = (aResult.bParsingSuccess != LocalParseResult.ValidityEnum.Missing_Critical);
            cell.Tag = null;
            row.Cells.Add(cell);

            // Filename
            cell = new DataGridViewTextBoxCell();
            cell.Value = aResult.PathPair.FileName;
            cell.Tag = null;
            row.Cells.Add(cell);

            // Serie
            cell = new DataGridViewTextBoxCell();
            cell.Value = aResult.GetValue(LocalParseResult.csSerieName);
            cell.Tag = LocalParseResult.csSerieName;
            row.Cells.Add(cell);

            // Season
            cell = new DataGridViewTextBoxCell();
            cell.Value = aResult.GetValue(LocalParseResult.csSeasonIndex);
            cell.Tag = LocalParseResult.csSeasonIndex;
            row.Cells.Add(cell);

            // Episode
            cell = new DataGridViewTextBoxCell();
            cell.Value = aResult.GetValue(LocalParseResult.csEpisodeIndex);
            cell.Tag = LocalParseResult.csEpisodeIndex;
            row.Cells.Add(cell);

            // Format
            cell = new DataGridViewTextBoxCell();
            cell.Value = aResult.GetValue(LocalParseResult.csEpisodeFormat);
            cell.Tag = LocalParseResult.csEpisodeFormat;
            row.Cells.Add(cell);

            // Team
            cell = new DataGridViewTextBoxCell();
            cell.Value = aResult.GetValue(LocalParseResult.csEpisodeTeam);
            cell.Tag = LocalParseResult.csEpisodeTeam;
            row.Cells.Add(cell);

            // Path
            cell = new DataGridViewTextBoxCell();
            cell.Value = aResult.PathPair.Path;
            cell.Tag = null;
            row.Cells.Add(cell);
            
            CheckColorBackGround(row);
            fileEditDataGridView.Rows.Add(row);
        }

        private void CheckColorBackGround(DataGridViewRow aRow)
        {
            LocalParseResult result = aRow.Tag as LocalParseResult;

            if (result != null)
            {
                switch (result.bParsingSuccess)
                {
                    case LocalParseResult.ValidityEnum.Complete:
                        aRow.Cells[0].Style.BackColor = m_colorComplete;
                        aRow.Cells[0].Value = true;
                        aRow.Cells[0].ReadOnly = false;
                        m_nbComplete++;
                        break;
                    case LocalParseResult.ValidityEnum.Missing_Optionnal:
                        aRow.Cells[0].Style.BackColor = m_colorOptional;
                        aRow.Cells[0].Value = true;
                        aRow.Cells[0].ReadOnly = false;
                        m_nbOptional++;
                        break;
                    case LocalParseResult.ValidityEnum.Missing_Critical:
                        aRow.Cells[0].Style.BackColor = m_colorCritical;
                        aRow.Cells[0].Value = false;
                        aRow.Cells[0].ReadOnly = true;
                        m_nbCritical++;
                        break;
                }

                string field;
                for (int i = 1; i < aRow.Cells.Count; i++)
                {
                    try
                    {
                        if (aRow.Cells[i].Tag == null)
                        {
                            aRow.Cells[i].Style.BackColor = m_colorComplete;
                        }
                        else
                        {
                            field = aRow.Cells[i].Tag as string;
                            if (field != null)
                            {
                                if (result.GetValidity(field))
                                    aRow.Cells[i].Style.BackColor = m_colorComplete;
                                else
                                    aRow.Cells[i].Style.BackColor = m_dicoColorColumn[field];
                            }
                        }
                    }
                    catch (Exception) {}
                }
            }

            GUIMain.LocalParsingUC.Step1UC.ReportProgress(LocalParsingManager.Step.Step1_4, m_nbComplete, m_nbComplete + m_nbCritical + m_nbOptional);
            GUIMain.LocalParsingUC.Step1UC.ReportProgress(LocalParsingManager.Step.Step1_4, "- " + m_nbOptional.ToString() + " fichier(s) avec au moins une information optionnelle manquante");
            GUIMain.LocalParsingUC.Step1UC.ReportError(LocalParsingManager.Step.Step1_4, "- " + m_nbCritical.ToString() + " fichier(s) avec au moins information indispensable manquante");
        }

        private void UpdateRow(DataGridViewRow aRow, int aColumnIndex, string aField, string aValue)
        {
            LocalParseResult result = aRow.Tag as LocalParseResult;

            if (result != null)
            {
                LocalParseResult.ValidityEnum oldValidity = result.bParsingSuccess;
                result.SetValue(aField, aValue);
                result.CheckValidity();

                aRow.Cells[aColumnIndex].Value = aValue;

                if (oldValidity == LocalParseResult.ValidityEnum.Complete)
                    m_nbComplete--;
                else if (oldValidity == LocalParseResult.ValidityEnum.Missing_Optionnal)
                    m_nbOptional--;
                else 
                {
                    m_nbCritical--;
                    if (oldValidity != result.bParsingSuccess)
                        aRow.Cells[0].Value = true;
                }
                
                CheckColorBackGround(aRow);
            }
        }

        private void UpdateModel(List<LocalParseResult> aList)
        {
            fileEditDataGridView.SuspendLayout();
            fileEditDataGridView.Rows.Clear();
            m_nbComplete = 0;
            m_nbOptional = 0;
            m_nbCritical = 0;

            foreach (LocalParseResult result in aList)
                AddItem(result);

            fileEditDataGridView.Sort(m_localParseResultDataRowComparer);

            fileEditDataGridView.ResumeLayout();
        }

        private List<LocalParseResult> GetModel()
        {
            List<LocalParseResult> resultsList = new List<LocalParseResult>();
            LocalParseResult result;

            foreach (DataGridViewRow row in fileEditDataGridView.Rows)
            {
                result = row.Tag as LocalParseResult;
                if (result != null)
                {
                    result.ToBeProcessed = (bool)row.Cells[0].Value;
                    resultsList.Add(result);
                }
            }

            return resultsList;
        }
        #endregion Privées

        #region Methodes Publiques
        public void StartFilesInfosEdition(List<LocalParseResult> aList)
        {
            UpdateModel(aList);
            this.Visible = true;
        }

        public List<LocalParseResult> StopFilesInfosEdition()
        {
            List<LocalParseResult> resultList = GetModel();
            this.Visible = false;
            fileEditDataGridView.Rows.Clear();
            return resultList;
        }

        public void CancelFilesInfosEdition()
        {
            this.Visible = false;
            fileEditDataGridView.Rows.Clear();
        }
        #endregion Methodes Publiques

        #region Event
        private void fileEditDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow currentRow = fileEditDataGridView.Rows[e.RowIndex];

            String value = String.Empty;

            String field = currentRow.Cells[e.ColumnIndex].Tag as String;

            if (field != null)
            {
                value = ((LocalParseResult)currentRow.Tag).GetValue(field);
                if (value != (string)currentRow.Cells[e.ColumnIndex].Value)
                {
                    value = (string)currentRow.Cells[e.ColumnIndex].Value;
                    foreach (int columnIndex in m_previousSelectedRows)
                        UpdateRow(fileEditDataGridView.Rows[columnIndex], e.ColumnIndex, field, value);
                }
            }
        }

        private void fileEditDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            m_previousSelectedRows.Clear();

            foreach (DataGridViewCell aCell in fileEditDataGridView.SelectedCells)
                if (aCell.ColumnIndex == m_previousSelectedColumn)
                    if (!m_previousSelectedRows.Contains(aCell.RowIndex))
                        m_previousSelectedRows.Add(aCell.RowIndex);
        }

        private void fileEditDataGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            if (fileEditDataGridView.CurrentCell != null)
            {
                m_previousSelectedColumn = fileEditDataGridView.CurrentCell.ColumnIndex;
                m_previousSelectedRow = fileEditDataGridView.CurrentCell.RowIndex;
            }
        }

        private void sortButton_Click(object sender, EventArgs e)
        {
            fileEditDataGridView.Sort(m_localParseResultDataRowComparer);
        }
        #endregion Event


    }
}
